package com.oinux.playhelper.ui;

import java.util.List;

import android.app.ActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;

import com.oinux.playhelper.R;
import com.oinux.playhelper.app.BaseActivity;
import com.umeng.analytics.MobclickAgent;

public class AppStart extends BaseActivity {

	private static final String TAG = "app_start_activity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		View view = View.inflate(this, R.layout.app_start, null);
		setContentView(view);
		ActivityManager mActivityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		List<ActivityManager.RunningServiceInfo> mServiceList = mActivityManager
				.getRunningServices(30);
		String serviceName = "com.oinux.playhelper.service.MainService";
		boolean start = false;
		for (int i = 0; i < mServiceList.size(); i++) {
			if (serviceName.equals(mServiceList.get(i).service.getClassName())) {
				start = true;
				break;
			}
		}
		if (start)
			showAnimation(view, 500);
		else
			showAnimation(view, 2000);
		MobclickAgent.setDebugMode(true); 
	}

	private void startMain() {
		Intent intent = new Intent(AppStart.this, MainActivity.class);
		startActivity(intent);
		finish();
	}

	private void showAnimation(View view, long misecond) {
		AlphaAnimation aa = new AlphaAnimation(0.3f, 1.0f);
		aa.setDuration(misecond);
		view.startAnimation(aa);
		aa.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationEnd(Animation arg0) {
				startMain();
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationStart(Animation animation) {
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
}
